﻿<?php
	defined('_JEXEC') or die('Restricted access'); 
?>
<?php		
	if(isset($this->product)){	
		$task = 'edit';
		$product = $this->product;				
	}
	else
		$task = 'addnew';
?>
<script type="text/javascript">
	$(document).ready(function(){
	$('#start').datetimepicker({
		onClose: function(dateText, inst) {
			var endDateTextBox = $('#end');
			if (endDateTextBox.val() != '') {
				var testStartDate = new Date(dateText);
				var testEndDate = new Date(endDateTextBox.val());
				if (testStartDate > testEndDate)
					endDateTextBox.val(dateText);
			}
			else {
				endDateTextBox.val(dateText);
			}
		},

		onSelect: function (selectedDateTime){
			var start = $(this).datetimepicker('getDate');
			$('#end').datetimepicker('option', 'minDate', new Date(start.getTime()));
		}
	});

	$('#end').datetimepicker({
		onClose: function(dateText, inst) {
			var startDateTextBox = $('#start');
			if (startDateTextBox.val() != '') {
				var testStartDate = new Date(startDateTextBox.val());
				var testEndDate = new Date(dateText);
				if (testStartDate > testEndDate)
					startDateTextBox.val(dateText);
			}
			else {
				startDateTextBox.val(dateText);
			}
		},
		onSelect: function (selectedDateTime){
			var end = $(this).datetimepicker('getDate');
			$('#start').datetimepicker('option', 'maxDate', new Date(end.getTime()) );
		}
	});
	});
</script>
<div class="wrapper wmain">
<div id="content">
    <div id="nav">
		<h2>Add New Product</h2>
        <ul>
			<li><a href="#product-add-general">Product general</a></li>
			<li><a href="#product-add-price">Price & Free Gift</a></li>
			<li><a href="#product-add-related">Related product</a></li>
			<li><a href="#product-add-category">Category</a></li>
        </ul>
	</div>	
	<div id="manager">
		
		<form id="adminForm" name="frmproduct" action="index.php?option=com_retailshop" method="post" enctype="multipart/form-data" onsubmit="val_product();">
		<div id="toolbar-box">
              <div class="toolbar">
                <ul>
					<li class="toolbar-save" id="saveproduct"><a href="#"><span class="icon-save"></span>Save</a></li>
					<li class="toolbar_cancel"><a href="index.php?option=com_retailshop&view=myshop&task=setting"><span class="icon-cancel"></span>Cancel</a></li>
                </ul>
              </div>
              <div class="title">
                <h2>Product Manager: Add New Product</h2>
              </div>
            </div>  
		<div id="product-add-general">            
			<div class="element">				
				<span id="err-product">					
				</span>
			</div>
			<div class="element">
				<label>Product Name:</label>
				<input type="text" name="pro-name" value="<?php echo $task=='edit' ? $product->name : ''; ?>" />
			</div>
			<div class="element">
				<label>Title</label>
				<input type="text" name="pro-title" value="<?php echo $task=='edit' ? $product->title : ''; ?>"/>
			</div>
			<div class="element">
				<label>Short description</label>
				<textarea type="text" name="pro-short-description"><?php echo $task=='edit' ? $product->short_description : ''; ?></textarea>
			</div>
			<div class="element">
				<label>Description</label>
				<textarea type="text" name="pro-description"><?php echo $task=='edit' ? $product->description : ''; ?></textarea>
			</div>
			<div class="element">
				<label>Image</label>
				<input type="file" width="auto" name="pro-image"/> 
			</div>
			<div class="element">
				<label>Status</label>
				<input type="checkbox" <?php if($task=='edit'){ echo ($product->status == 1)?'checked':''; } ?>  name="pro-status" /> <span>Publish</span>
			</div>
			<div class="element">
				<label>Sort Order:</label>
				<input type="text" placeholder="" name="pro-order" value="<?php echo $task=='edit' ?  $product->order : ''; ?>">	
			</div>
			<div><a href="#" id="nextstep_2" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>			
		</div>          
		<div id="product-add-price">
			<div class="element">
				<label>Price</label>
				<input type="text" name="pro-price"value="<?php echo $task=='edit' ? $product->price : ''; ?>"/>
			</div>
			<div class="element">
				<label>Free Gift</label>
				<input type="text"name="pro-free-gift" value="<?php echo $task=='edit' ? $product->free_gift : ''; ?>" />
			</div>		
			<div class="element">
				<label>Promotion</label>
				<input type="text" name="pro-promotion" value="<?php echo $task=='edit' ? $product->promotion : ''; ?>" />
			</div>
			<div class="element">
				<label>Time</label>
				<input type="text" name="start" id="start" class="time" value="<?php echo $task=='edit' ? $product->date_start_promotion : ''; ?>"/> to
				<input type="text" name="end" id="end" class="time" value="<?php echo $task=='edit' ? $product->date_end_promotion: ''; ?>"/>
			</div>
			<div><a href="#" id="nextstep_3" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>
			<div><a href="#" id="nextstep_1" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>			
		</div>
		<div id="product-add-related">
			<table width="100%">
				<thead>
					<tr>
						<th width="5%"><input type="checkbox" name="checkall" id="checkall" /></td>
						<th width="45%">Name</td>
						<th width="50%">Title</td>					
					</tr>					
				</thead>
				<tbody>
					<?php foreach($this->products as $pro) {?>
					<tr>
						<td><input type="checkbox" name="check[]" value="<?php echo $pro->id; ?>"/></td>
						<td><?php echo $pro->name; ?></td>
						<td><?php echo $pro->title; ?></td>	
					</tr>
					<?php } ?>
				</tbody>
				<tfoot>
					<tr>
					   <td colspan="4">
						   <!-- <div class="pagination">
							 <div class="limit">Display #
							   <select size="1" class="inputbox" name="limit" id="limit">
								 <option value="5">5</option>
								 <option value="10">10</option>
								 <option value="15">15</option>
								 <option selected="selected" value="20">20</option>
								  <option value="25">25</option>
								 <option value="30">30</option>
								 <option value="50">50</option>
								 <option value="100">100</option>
								  <option value="0">All</option>
							   </select>
							 </div>
							</div> -->
						 </td>
					 </tr>
					</tfoot>
				</table>
<div><a href="#" id="nextstep_4" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>	
<div><a href="#" id="nextstep_2" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>				
			  </div>
			<div id="product-add-category">		
				<table width="100%">
					<thead>
					<tr>
						<th width="5%"><input type="checkbox" name="checkcateall" id="checkall" /></td>
						<th width="95%">Name</td>
					</tr>					
				</thead>
				<tbody>
				<?php if($this->catolog == null){ ?>
				<?php }else { ?>
				<?php foreach($this->catolog as $cate){ ?>
					<tr>
						<td><input type="checkbox" name="checkcate[]" value="<?php echo $cate->id; ?>" /></td>
						<td><?php echo $cate->name; ?></td>					
					</tr>
				<?php } } ?>
				</tbody>
					<tfoot>
						<tr>
						<td colspan="4">
							<!-- <div class="pagination">
							  <div class="limit">Display #
								<select size="1" class="inputbox" name="limit" id="limit">
								  <option value="5">5</option>
								  <option value="10">10</option>
								  <option value="15">15</option>
								  <option selected="selected" value="20">20</option>
								  <option value="25">25</option>
								  <option value="30">30</option>
								  <option value="50">50</option>
								  <option value="100">100</option>
								  <option value="0">All</option>
								</select>
							  </div>
							</div> -->
						  </td>
					  </tr>
					</tfoot>
				</table>
				<div><a href="#" id="nextstep_3" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>
			</div>	
			<input type="hidden" name="view" value="myshop" />
			<input type="hidden" name="task" value="save" />			
			<input type="hidden" name="action" value="<?php echo $task=='edit' ? 'edit' : 'addnew'?>" />
			<?php echo $task=='edit' ? '<input type="hidden" name="productid" value="'.$product->id.'" />' : ''?>
			<?php echo $task!='edit' ? '<input type="hidden" name="shop_id" value="'.$_GET['shop'].'" />' : ''?>
		</form>
		
	</div>
</div>	
</div>
<script type="text/javascript">
	$("#content #nav ul").idTabs();
	$('#saveproduct').click(function(){
		var prod_name = $('input:text[name="pro-name"]').val();
		var prod_title = $('input:text[name="pro-title"]').val();
		var prod_short_description = $('textarea[name="pro-short-description"]').val();		
		var prod_file = $('input:file[name="pro-image"]').val();
		var prod_price = $('input:text[name="pro-price"]').val();		
		var prod_cate = $('input:checkbox[name="checkcate[]"]:checked');
		var err = '';
		var extension = prod_file.split('.').pop().toLowerCase();
		if(prod_name=='')
			err +='<p>Please enter nameproduct</p>';
		if(prod_title=='')
			err +='<p>Please enter title</p>';
		if(prod_short_description=='')
			err +='<p>Please enter short description</p>';
		if(prod_price=='')
			err +='<p>Please enter price product</p>';
		if(prod_cate.length < 1)
			err +='<p>Please choose catelogy product</p>';
		<?php if($task!="edit"){ ?>
		if( prod_file=='' || $.inArray(extension, ['png', 'gif', 'jpeg', 'jpg']) == -1){
			err +='<p>Please choose product image</p>';
		}
		<? } ?>
		if(err.length != 0){		
			
			$('#err-product').html(err);
			$('#err-product').addClass('validate-form');
			$("a[href='#product-add-general']").click();
			return false;
		}else{
			$('#err-product').html('');
		}
		document.frmproduct.submit();
		return false;
	});
	function nextstep(id){		
		var arr = $(id).attr('id').split('_');
		var step = arr[1];			
		switch(step){			
			case '1':				
				$("a[href='#product-add-general']").click();
				break;
			case '2':
				$("a[href='#product-add-price']").click();
				break;
			case '3':
				$("a[href='#product-add-related']").click();
				break;
			case '4':
				$("a[href='#product-add-category']").click();				
				break;
			default:
				$("a[href='#product-add-general']").click();
				break;				
		}		
		return	false;
	}	
</script>
<?php if($task=='edit') { ?>
	<script>		
		var rela_product = $('input:checkbox[name="check[]"]');
		<?php foreach($this->relatedproduct as $a) { ?>
			for(var j=0; j < rela_product.length; j++){
				var id = <?php echo $a->id; ?>;				
				if(rela_product[j].value == id){
					$(rela_product[j]).attr('checked', true);
					continue;
				}
			}
		<?php } ?>
		
		var rela_cate = $('input:checkbox[name="checkcate[]"]');
		
		<?php foreach($this->catologproduct as $a) { ?>		
			for(var j=0; j < rela_cate.length; j++){
				var id = <?php echo $a->retailshop_category_product_id; ?>;								
				if(rela_cate[j].value == id){
					
					$(rela_cate[j]).attr('checked', true);
					continue;
				}
			}
		<?php } ?>
	</script>
<?php }?>